//
//  ShowAlertTool.m
//  eyibao
//
//  Created by jouhu on 2017/8/9.
//  Copyright © 2017年 jouhu. All rights reserved.
//

#import "ShowAlertTool.h"


@implementation ShowAlertTool

#pragma mark - 确认键AlertVC弹窗选择(单键)
//弹窗选择(单键,有回调)
+ (void)yh_showMyAlertTitle:(NSString*)title andMessage:(NSString *)message OKCompletion:(alertCompletionBlock)OKCompletion{
    
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    //确定
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定"  style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        //确认键回调
        if (OKCompletion) {
            OKCompletion();
        }
    }];
    
    [alertController addAction:okAction];
    
    //显示
    
    [[self theTopviewControler]  presentViewController:alertController animated:YES completion:nil];
    
    
}

//弹窗选择(单键,无回调)
+ (void)yh_showMyAlertTitle:(NSString*)title andMessage:(NSString *)message{
    
    [self yh_showMyAlertTitle:title andMessage:message OKCompletion:nil];
}


#pragma mark - 确认和取消双键AlertVC弹窗选择(双键-StyleAlert模式)

//弹窗选择(双键,有回调-自定义按钮文字)
+ (void)yh_showMyRemindAlertTitle:(NSString*)title andMessage:(NSString *)message btn1Title:(NSString *)btn1Title btn2Title:(NSString *)btn2Title OKCompletion:(alertCompletionBlock)OKCompletion cancelCompletion:(alertCompletionBlock)cancelCompletion{
    
    if (btn1Title == nil) {
        btn1Title = @"确定";
    }
    
    if (btn2Title == nil) {
        btn2Title = @"取消";
    }
    
    //创建alert
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    //ok键
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:btn1Title style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        //确认键回调
        if (OKCompletion) {
            OKCompletion();
        }
    }];
    
    //取消键
    UIAlertAction *refuseAction = [UIAlertAction actionWithTitle:btn2Title style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        //取消键回调
        if (cancelCompletion) {
            cancelCompletion();
        }
    }];
    
    //添加按键
    [alertController addAction:refuseAction];
    [alertController addAction:okAction];
    
    //显示
    [[self theTopviewControler]  presentViewController:alertController animated:YES completion:nil];
}


//弹窗选择(双键,有回调-默认安妞确认/取消)
+ (void)yh_showMyRemindAlertTitle:(NSString*)title andMessage:(NSString *)message OKCompletion:(alertCompletionBlock)OKCompletion cancelCompletion:(alertCompletionBlock)cancelCompletion{
    
    [self yh_showMyRemindAlertTitle:title andMessage:message btn1Title:nil btn2Title:nil OKCompletion:OKCompletion cancelCompletion:cancelCompletion];
    
}

//弹窗选择(双键,无回调)
//+ (void)yh_showMyRemindAlertTitle:(NSString*)title andMessage:(NSString *)message{
//
//    [self yh_showMyRemindAlertTitle:title andMessage:message OKCompletion:nil cancelCompletion:nil];
//}

#pragma mark - 确认和取消双键AlertVC弹窗选择(双键-StyleActionSheet模式)
//弹窗选择(双键-StyleActionSheet模式-有回调-设置按钮文字)
+ (void)yh_showMyRemindAlertStyleActionSheetTitle:(NSString*)title andMessage:(NSString *)message btn1Title:(NSString *)btn1Title btn2Title:(NSString *)btn2Title OKCompletion:(alertCompletionBlock)OKCompletion  cancelCompletion:(alertCompletionBlock)cancelCompletion{
    
    if (btn1Title == nil) {
        btn1Title = @"确定";
    }
    
    if (btn2Title == nil) {
        btn2Title = @"取消";
    }
    
    //创建alert
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
    
    //1键
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:btn1Title style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        //1键回调
        if (OKCompletion) {
            OKCompletion();
        }
    }];
    
    //2键
    UIAlertAction *refuseAction = [UIAlertAction actionWithTitle:btn2Title style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        //2键回调
        if (cancelCompletion) {
            cancelCompletion();
        }
    }];
    
    //添加按键
    [alertController addAction:refuseAction];
    [alertController addAction:okAction];
    
    //显示
    [[self theTopviewControler]  presentViewController:alertController animated:YES completion:nil];
}
//弹窗选择(双键-StyleActionSheet模式-有回调-默认确认/取消安妞)
+ (void)yh_showMyRemindAlertStyleActionSheetTitle:(NSString*)title andMessage:(NSString *)message OKCompletion:(alertCompletionBlock)OKCompletion  cancelCompletion:(alertCompletionBlock)cancelCompletion{
    
    [self yh_showMyRemindAlertStyleActionSheetTitle:title andMessage:message btn1Title:nil btn2Title:nil OKCompletion:OKCompletion cancelCompletion:cancelCompletion];
}

////弹窗选择(双键-StyleActionSheet模式-无回调-设置按钮文字)
//+ (void)yh_showMyRemindAlertStyleActionSheetTitle:(NSString*)title andMessage:(NSString *)message btn1Title:(NSString *)btn1Title btn2Title:(NSString *)btn2Title{
//
//    [self yh_showMyRemindAlertStyleActionSheetTitle:title andMessage:message btn1Title:btn1Title btn2Title:btn2Title OKCompletion:nil cancelCompletion:nil];
//}

////弹窗选择(双键-StyleActionSheet模式-无回调-默认确认/取消安妞)
//+ (void)yh_showMyRemindAlertStyleActionSheetTitle:(NSString*)title andMessage:(NSString *)message{
//
//    [self yh_showMyRemindAlertStyleActionSheetTitle:title andMessage:message btn1Title:nil btn2Title:nil OKCompletion:nil cancelCompletion:nil];
//}




+ (UIViewController *)theTopviewControler{
    UIViewController *rootVC = [[UIApplication sharedApplication].delegate window].rootViewController;
    
    UIViewController *parent = rootVC;
    
    while ((parent = rootVC.presentedViewController) != nil ) {
        rootVC = parent;
    }
    
    while ([rootVC isKindOfClass:[UINavigationController class]]) {
        rootVC = [(UINavigationController *)rootVC topViewController];
    }
    
    return rootVC;
}



@end
